package com.github.wui.circular.circularPointer;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.wui.R;


/**
 * 环形指示器
 * <p>
 * <p>
 * 组件原理 ： 使用两个圆环组成 半径不一样
 * 圆环使用圆来绘制
 * <p>
 *
 * @author Mr.xie
 * @Date 2025/5/15
 */
public class CircularPointer extends View {


    /**
     * 控件默认高
     */
    private int viewHeight = 200;


    /**
     * 控件默认宽
     */
    private int viewWidth = 400;


    /**
     * 轨道画笔
     */
    private Paint progressTrackPaint = new Paint();


    private int progressTrackColor = Color.GRAY;


    /**
     * 轨道画笔
     */
    private Paint fillPaint = new Paint();


    private int fillColor = Color.BLACK;

    /**
     * 粗细
     */
    private int LineWidth = 20;


    /**
     * 最大值
     */
    private int max = 100;

    private int sweep_end_angle = 0;

    private float progress = 0;


    private Paint textPaint = new Paint();

    private int textColor = Color.WHITE;

    private float textSize = 30f;
    /**
     * 描述文字
     */
    private String text = "文字描述";

    private float textPosX = 0;
    private float textPosY = 0;

    public CircularPointer(Context context) {
        this(context, null);
    }

    public CircularPointer(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircularPointer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularPointer);

        this.progress = typedArray.getFloat(R.styleable.CircularPointer_progress, 0f);
        this.sweep_end_angle = typedArray.getInteger(R.styleable.CircularPointer_sweep_end_angle, 0);
        this.fillColor = typedArray.getColor(R.styleable.CircularPointer_fillColor, Color.BLACK);
        this.progressTrackColor = typedArray.getColor(R.styleable.CircularPointer_progressTrackColor, Color.GRAY);
        this.text = typedArray.getString(R.styleable.CircularPointer_text);
        this.textSize = typedArray.getDimension(R.styleable.CircularPointer_textSize, 20f);
        this.textColor = typedArray.getColor(R.styleable.CircularPointer_textColor, Color.WHITE);
        this.max = typedArray.getInteger(R.styleable.CircularPointer_maxValue, 10);
        this.LineWidth = (int) typedArray.getDimension(R.styleable.CircularPointer_trackWidth , 20f);
        this.textPosX = typedArray.getDimension(R.styleable.CircularPointer_text_pos_x,0f);
        this.textPosY = typedArray.getDimension(R.styleable.CircularPointer_text_pos_y,0f);
        typedArray.recycle();
        init();
    }


    /**
     * 初始化
     */
    private void init() {
        progressTrackPaint.setAntiAlias(true);
        progressTrackPaint.setStrokeWidth(LineWidth);
        progressTrackPaint.setStyle(Paint.Style.STROKE);
        progressTrackPaint.setStrokeCap(Paint.Cap.ROUND);
        progressTrackPaint.setColor(progressTrackColor);


        fillPaint.setAntiAlias(true);
        fillPaint.setStrokeWidth(LineWidth);
        fillPaint.setStyle(Paint.Style.STROKE);
        fillPaint.setStrokeCap(Paint.Cap.ROUND);
        fillPaint.setColor(fillColor);

        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT || layoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT) {
            height = viewHeight;
        }
        if (layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT || layoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT) {
            width = viewWidth;
        }
        setMeasuredDimension(width, height);

    }


    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        initTrack(canvas);
        initFill(canvas);

        Point point = new Point();
        point.x = (int) (getMeasuredWidth() / 2);
        point.y = getMeasuredHeight() - getMeasuredHeight() / 4;
        drawText(canvas, text, point);
    }


    private int stepAngle = 0;

    private void initFill(Canvas canvas) {

        int w = getMeasuredWidth();
        stepAngle = (int) (progress * (180 / max));
        int top = getMeasuredHeight() / 2;

        if (stepAngle >= 180) {
            stepAngle = 160;
        }
        RectF rectF = new RectF(10, top, w - 10, getMeasuredHeight());
        canvas.drawArc(rectF, -180 + Math.abs(-10), stepAngle + sweep_end_angle, false, fillPaint);
    }

    private void initTrack(Canvas canvas) {

        int w = getMeasuredWidth();
        int top = getMeasuredHeight() / 2;

        int left = 0;
        int right = 0;
        RectF rectF = new RectF(10 + left, top, w - 10 - right, getMeasuredHeight());
        canvas.drawArc(rectF, -10, -160 + sweep_end_angle, false, progressTrackPaint);

    }


    private void drawText(Canvas canvas, String text, Point point) {
        textPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, point.x + textPosX, point.y + textPosY, textPaint);
    }


    public int getViewHeight() {
        return viewHeight;
    }

    public void setViewHeight(int viewHeight) {
        this.viewHeight = viewHeight;
        invalidate();
    }

    public int getViewWidth() {
        return viewWidth;
    }

    public void setViewWidth(int viewWidth) {
        this.viewWidth = viewWidth;
        invalidate();
    }

    public int getProgressTrackColor() {
        return progressTrackColor;
    }

    public void setProgressTrackColor(int progressTrackColor) {
        this.progressTrackColor = progressTrackColor;
        invalidate();
    }

    public int getFillColor() {
        return fillColor;
    }

    public void setFillColor(int fillColor) {
        this.fillColor = fillColor;
        invalidate();
    }


    public int getLineWidth() {
        return LineWidth;
    }

    public void setLineWidth(int lineWidth) {
        this.LineWidth = lineWidth;

    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        invalidate();
    }

    public int getSweep_end_angle() {
        return sweep_end_angle;
    }

    public void setSweep_end_angle(int sweep_end_angle) {
        this.sweep_end_angle = sweep_end_angle;
        invalidate();
    }

    public float getProgress() {
        return progress;
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    public int getTextColor() {
        return textColor;

    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        invalidate();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }
}
